﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ECCSystem.CheckClass;
using ECCSystem.BizRule;
using System.Diagnostics;
using System.Net.NetworkInformation;
using ECCSystem.UserControls;
namespace ECCSystem
{
	public partial class frmOverBill : Form
	{
		string strUserName = "";
		string strUserID = "";
		string strUserIP = "";
		int intMode = 0;
		public frmOverBill()
		{
			InitializeComponent();
		}

		private void frmOverBill_Load(object sender, EventArgs e)
		{
			BindData();
		}

		private bool Check()
		{
			DateTime dtStart = new DateTime();
			DateTime dtEnd = new DateTime();

			dtStart = dtpStart.Value;
			dtEnd = dtpEnd.Value;

			if (dtStart >= dtEnd)
			{
				MessageBox.Show("終了日付は開始日付より遅い！", "ECC Information");
				return false;
			}

			if (txtOverTimeUser.Text.Trim() == "")
			{
				MessageBox.Show("ユーザは選択されていません！", "ECC Information");
				return false;
			}

			if (intMode == 0)
			{
				if (strUserID.Trim() == "")
				{
					MessageBox.Show("ユーザは選択されていません！", "ECC Information");
					return false;
				}
			}

			return true;
		}

		#region 
		//private void btnAdd_Click(object sender, EventArgs e)
		//{
		//    //for (int i = 0; i < objAL.Count; i++)
		//    //{
		//    //    string strNetSend = "net send " + objAL[i].ToString() + 
		//    //                        " you will OverTime from " + 
		//    //                        dtpStart.Value.ToLongTimeString() + " to " + dtpEnd.Value.ToLongTimeString();

		//    //    Process process = new Process();//创建进程对象
		//    //    ProcessStartInfo startinfo = new ProcessStartInfo();//创建进程时使用的一组值，如下面的属性
		//    //    startinfo.FileName = "cmd.exe";//设定需要执行的命令程序... //以下是隐藏cmd窗口的方法
		//    //    startinfo.Arguments = "/c" + strNetSend;//设定参数，要输入到命令程序的字符，其中“/c"表示执行完命令后马上退出
		//    //    startinfo.UseShellExecute = false;//不使用系统外壳程序启动
		//    //    startinfo.RedirectStandardInput = false;//不重定向输入
		//    //    startinfo.RedirectStandardOutput = true;//重定向输出，而不是默认的显示在dos控制台上
		//    //    startinfo.CreateNoWindow = true;//不创建窗口
		//    //    process.StartInfo = startinfo;
		//    //    try
		//    //    {
		//    //        if (process.Start() == false)//开始进程
		//    //        {
		//    //        }
		//    //        else
		//    //        {
		//    //            objAL.RemoveAt(i);
		//    //            objALName.RemoveAt(i);
		//    //        }
		//    //    }
		//    //    catch (Exception ex)
		//    //    {

		//    //    }
		//    //    finally
		//    //    {
		//    //        if (process != null)
		//    //        {
		//    //            process.Close();
		//    //        }
		//    //    }

		//    //}
		//    //string str = "";
		//    //for (int j = 0; j < objALName.Count; j++)
		//    //{
		//    //    str = str + objALName[j].ToString() + ";";
		//    //}

		//    //if (str.Length > 0)
		//    //{
		//    //    MessageBox.Show("用户" + str + " 消息发送失败", "ECC Information");
		//    //}

		//    BindData();
		//}
		#endregion

		private void BindData()
		{
			ClsOverTimeBill obj = new ClsOverTimeBill();
			DataSet dt = (DataSet)obj.GetList();

			if (dt.Tables.Count > 0)
			{
				dtvUser_CellClick(null, null);

				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}

			if (dt.Tables.Count == 0)
			{
				return;
			}
			dtvUser.AutoGenerateColumns = false;
			dtvUser.DataSource = dt.Tables[0];

			if (dtvUser.Rows.Count > 0)
			{
				dtpStart.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["StartDate"].Value);
				dtpEnd.Value = Convert.ToDateTime(dtvUser.Rows[0].Cells["EndTime"].Value);
				txtOverTimeUser.Text = Convert.ToString(dtvUser.Rows[0].Cells["UserName"].Value);
				txtOverTimeUser.Tag = Convert.ToString(dtvUser.Rows[0].Cells["ID"].Value);
			}

		}

		private void frmOverBill_FormClosed(object sender, FormClosedEventArgs e)
		{
			Form frmMain = new frmMain();
			frmMain.Show();
		}

		private void btnAddUser_Click(object sender, EventArgs e)
		{
			DigUser objForm = new DigUser();
			if (txtOverTimeUser.Text != "")
			{
				objForm.PersonID = Convert.ToString(txtOverTimeUser.Tag);
				objForm.PersonName = txtOverTimeUser.Text;
			}

			objForm.ShowDialog(this);
			strUserName = objForm.PersonName;
			strUserID = objForm.PersonID;
			strUserIP = objForm.PersonIP;
			this.txtOverTimeUser.Text = strUserName;
			txtOverTimeUser.Tag = strUserID;
		}

		private void btnSave_Click(object sender, EventArgs e)
		{

			ClsOverTimeBill obj = new ClsOverTimeBill();
			//string strUser = "";

			if (Check() == false)
			{
				return;
			}

			if (intMode == 0)
			{
				obj.AddOverTimeBill(dtpStart.Value, dtpEnd.Value, strUserName, strUserID);
			}
			else
			{
				obj.UpdateOverTimeBill(Convert.ToString(dtvUser.SelectedRows[0].Cells["KeyID"].Value),
										  dtpStart.Value,dtpEnd.Value,txtOverTimeUser.Text.Trim(),
										  txtOverTimeUser.Tag.ToString().Trim());
				intMode = 0;
			}

			this.btnDelete.Enabled = true;
			this.btnUpdata.Enabled = true;
			this.dtvUser.Enabled = true;
			this.txtOverTimeUser.Text = "";
			this.BindData();
			ResetDateTime();
			
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			//wether have selected row,if have not selected row, pop-up prompt
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}
			else
			{
				if (MessageBox.Show("削除します、よろしいですか？", "ECC Information", MessageBoxButtons.OKCancel) == DialogResult.OK)
				{
					ECCSystem.BizRule.ClsOverTimeBill objBRMC = new ECCSystem.BizRule.ClsOverTimeBill();
					objBRMC.DeleteOverTimeBill(Convert.ToString(dtvUser.SelectedRows[0].Cells["KeyID"].Value));
					this.BindData();

					dtpStart.Value = DateTime.Now;
					dtpEnd.Value = DateTime.Now;
					txtOverTimeUser.Text = "";
					txtOverTimeUser.Tag = "";
				}

			}
			ResetDateTime();
		}

		private void btnUpdata_Click(object sender, EventArgs e)
		{
			if (dtvUser.CurrentRow == null)
			{
				MessageBox.Show("データを選んでください！", "ECC Information");
				return;
			}

			//dtpStart.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["StartDate"].Value);
			//dtpEnd.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["EndTime"].Value);
			//txtOverTimeUser.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["UserName"].Value);
			//txtOverTimeUser.Tag = Convert.ToString(dtvUser.SelectedRows[0].Cells["ID"].Value);

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intMode = 1;
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			this.txtOverTimeUser.Text = "";
			ResetDateTime();

			if (dtvUser.Rows.Count > 0)
			{
				this.btnDelete.Enabled = true;
				this.btnUpdata.Enabled = true;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;

				dtvUser.Rows[0].Selected = true;
				dtpStart.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["StartDate"].Value);
				dtpEnd.Value = Convert.ToDateTime(dtvUser.CurrentRow.Cells["EndTime"].Value);
				txtOverTimeUser.Text = Convert.ToString(dtvUser.CurrentRow.Cells["UserName"].Value);
				txtOverTimeUser.Tag = Convert.ToString(dtvUser.CurrentRow.Cells["ID"].Value);
			}
			else
			{
				this.btnDelete.Enabled = false;
				this.btnUpdata.Enabled = false;
				this.dtvUser.Enabled = true;
				this.btnAdd.Enabled = true;
				this.btnCancel.Enabled = false;
				this.btnSave.Enabled = false;
			}
		}

		private void ResetDateTime()
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
		}

		private void dtvUser_CellClick(object sender, DataGridViewCellEventArgs e)
		{
			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
			txtOverTimeUser.Text = "";
			txtOverTimeUser.Tag = "";

			if (dtvUser.SelectedRows.Count <= 0)
			{
				return;
			}
			dtpStart.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["StartDate"].Value);
			dtpEnd.Value = Convert.ToDateTime(dtvUser.SelectedRows[0].Cells["EndTime"].Value);
			txtOverTimeUser.Text = Convert.ToString(dtvUser.SelectedRows[0].Cells["UserName"].Value);
			txtOverTimeUser.Tag = Convert.ToString(dtvUser.SelectedRows[0].Cells["ID"].Value);
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{

			dtpStart.Value = DateTime.Now;
			dtpEnd.Value = DateTime.Now;
			txtOverTimeUser.Text = "";
			txtOverTimeUser.Tag = "";

			this.btnDelete.Enabled = false;
			this.btnUpdata.Enabled = false;
			this.dtvUser.Enabled = false;
			this.btnAdd.Enabled = false;
			this.btnCancel.Enabled = true;
			this.btnSave.Enabled = true;

			intMode = 0;
		}


	}
}